package com.xuzmian.demo.kotlin

import org.junit.Test

/**
 * 语法演示类
 * @author xzm
 */
class GrammerDemo {

    //循环：loop
    @Test
    fun `test Grammer for Loop`() {
        //设定循环步长
        for (x: Int in 9 downTo 0 step 3) {
            print("$x ' '")
        }

        println()
        for (letter in "abcdefg") {
            print("$letter ' '")
        }

        println()
        for (index in list.indices) {
            print("$index ' '")
        }

        println()
        for (item in list) {
            print("$item ' '")
        }

        println()
        //区间循环：range loop
        for (i in 1..4) {
            print("$i ' '")
        }

        println()
        for ((index, value) in list.withIndex()) {
            print("the element at $index is $value")
        }

        println()
        for (item in map) {
            print("${item.key} : ${item.value}  | ")
        }

        println()
        for ((k, v) in map) {
            print("$k : $v  | ")
        }
    }

    //条件表达式
    @Test
    fun `test Grammer for if`() = if(arr1.size > 2) print("arr1.size more than two") else print("arr1.size not more than two")


    //when 也可以用来取代 if-else if链。 如果不提供参数，所有的分支条件都是简单的布尔表达式，而当一个分支的条件为真时则执行该分支
    @Test
    fun `test Grammer for when`() {
        when {
            list.size > 5 -> print("list size more than five")
            list.isEmpty() -> print("list is empty")
            else -> print("list not more than five and not empty")
        }
    }

}